import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        token: '',
        username: ''
    },
    mutations: {
        // 用vuex存储token
        setToken(state, payload) {
            state.token = payload;
            window.localStorage.setItem("myToken", payload)
        },
        setUsername(state, payload) {
            state.username = payload;
            window.localStorage.setItem('username', payload)
        },
        clearToken(state) {
            window.localStorage.removeItem('myToken')
        }
    },
    getters: {
        getToken(state) {
            if (state.token) {
                return state.token
            }
            return localStorage.getItem('myToken')
        },
        getUsername(state) {
            if (state.username) {
                return state.username
            }
            return localStorage.getItem('username')
        }
    },
    modules: {}
})